<?php
/**
 * Created by PhpStorm.
 * User: blue
 * Date: 2018/1/27
 * Time: 9:57
 */

namespace app\auth\controller;


use app\common\controller\Base;
use libs\Arr;
use think\Db;
use think\Validate;
use think\View;

class Rule extends Base
{
    protected $modelName='rule';
    protected $pk='id';
    // public function beforeAddC(){
    //     $data=Db::table('addons_auth_rule')->field('id,pid,title')->select();
    //     $data=(new Arr)->tree($data,'title','id');
    //     isset($data)&&\think\View::share('relData',$data);
    // }
    public function listC(){
        //定义前置方法
        method_exists(__CLASS__, 'beforeListC')&& $this->beforeListC();
        $res=$this->model->show();
        $data=(new Arr)->tree($res['data'],'title',$this->pk);
        // 如果查到数据,则分配变量
        isset($res['data'])&&\think\View::share('data',$data);
        return view();
    }

    public function delC(){
        $data=$this->request->param();
        //验证部分
        $rule=['id'=>'require|token'];
        $msg=['id.require'=>'id没有值'];
        $validate=Validate::make($rule,$msg);
        $check=$validate->check($data);
        if(!$check){
            return json($validate->getError());
        }
        $res=$this->model->del($data['id']);
        return $res['data']?json(['status'=>'success','msg'=>'删除成功']):json(['status'=>'error','msg'=>"删除失败"]);
    }

    public function addC(){
        // 查找相关表中的旧数据
        $pdata=Db::table('addons_auth_rule')->field('id,pid,title')->select();
        $dataTree=(new Arr())->tree($pdata,'title','id');
        View::share('parent',$dataTree);
        if($this->request->isPost()){
            $data=$this->request->param('','','strip_tags,trim');
            $check=$this->validation('\aop\validate\Rule',$data,'add');
            if($check['status']===0){
                $this->error($check['msg']);
            }
            $res=$this->model->add($data);
            echo $this->model->getLastSql();
            ($res['status']=='success')?$this->success($res['msg'],'listC'):$this->error($res['msg'],'listC');
        }
        return view();
    }

    public function editC(){
        //获取id,分配旧数据,注意父规则的分配
        $where[$this->pk]=intval($this->request->param('id'));
        $oldData=Db::table('addons_auth_rule')->where($where)->select();
        //编辑的父规则,不可以为自身和子规则
        $tmp=Db::table('addons_auth_rule')->select(); //数据库读取整个规则表
        $treeParent=(new Arr())->tree_notchild($tmp,'title',$where[$this->pk],'id');
        View::share(['oldData'=>$oldData[0],'parent'=>$treeParent]);
        //获取新数据,并入库
        if($this->request->isPost()){
            $data=$this->request->param('','','strip_tags,trim');
            //使用验证器进行数据验证
            $check=$this->validation('\aop\validate\Rule',$data,'edit');
            if($check['status']==0){
                $this->error($check['msg']);
            }
            //模型中的编辑方法
            $res=$this->model->edit($data);
            ($res['status']=='success')?$this->success($res['msg'],'listC'):$this->error($res['msg'],'listC');
        }
        return view();
    }
}